package com.foreknow.tijian.dao.Impl;

import com.foreknow.tijian.dao.FoodDao;
import com.foreknow.tijian.po.Food;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    //连接数据库
    Connection con= DBUtil.getConnection();
    PreparedStatement pst;
    @Override
    public List<Food> listFoodByBusinessId(int businessId) {
        String sql="select * from food where businessId=?";
        List<Food> foodList=new ArrayList<Food>();

        try {
            //获取与处理对象
            pst=con.prepareStatement(sql);
            pst.setInt(1, businessId);
            ResultSet rs=pst.executeQuery();//执行查询
            //结果进行遍历
            while(rs.next()){
                Food food=new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                foodList.add(food);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            //关闭资源
            DBUtil.close(pst);
        }
        return foodList;
    }
}
